package com.liangxiao.wifichat;

import android.app.*;
import android.content.Context;
import android.os.*;
import android.util.Log;
import android.view.*;
import android.widget.*;
import android.net.wifi.*;
import com.liangxiao.wifichat.fragment.ChatRoomFragment;
import com.liangxiao.wifichat.fragment.MainFragment;
import com.liangxiao.wifichat.model.BaseMessage;

public class MainActivity extends Activity {

    private WifiChatHandler mHandler;
    private MainFragment mainFragment;
    private ChatRoomFragment chatRoomFragment;
    private WifiChatService service;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mHandler = new WifiChatHandler(this);
        service = WifiChatService.getInstance(this, mHandler);

        mainFragment = MainFragment.getInstance(service);
        chatRoomFragment = ChatRoomFragment.getInstance(this, mHandler);

        /*Button open = (Button) findViewById(R.id.open);
        open.setOnClickListener(this);

        Button reset = (Button) findViewById(R.id.reset);
        reset.setOnClickListener(this);

        Button close = (Button) findViewById(R.id.close);
        close.setOnClickListener(this);

        Button send = (Button) findViewById(R.id.send);
        send.setOnClickListener(this);

        Button connectWIFI = (Button)findViewById(R.id.connect_wifi);
        connectWIFI.setOnClickListener(this);

		Button connect = (Button)findViewById(R.id.connect);
		connect.setOnClickListener(this);*/

        getFragmentManager().beginTransaction()
                .replace(R.id.main_container, mainFragment)
                .addToBackStack("MainFragment")
                .commit();

    }
    private void toChatRoom(){
        getFragmentManager().beginTransaction()
                .replace(R.id.main_container, chatRoomFragment)
                //.addToBackStack("ChatRoomFragment")
                .commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        service.getApManager().closeAp(true);
        service.getApManager().closeWifi();
    }

    /*

    @Override
    public void onClick(View p1) {
        switch (p1.getId()) {
            case R.id.close:
                ap.closeAp(false);
                break;
            case R.id.send:
                mService.write(msg.getText().toString().getBytes());
                break;
            case R.id.open:
                ap.openAP(et.getText().toString(), "123456789");
                if (mService.getState() == WifiChatService.STATE_NONE) {

                    mService.start();
                }
                break;
            case R.id.connect_wifi:
                boolean b = ap.connect(et.getText().toString(), "123456789");
                Toast.makeText(this, b + "正在连接 " + et.getText().toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.connect:
                showToast("server ip :" + ap.getServerIpAdress());
                mService.connect(ap.getServerIpAdress());
                break;

            case R.id.reset:
                ap.resetAp();
                break;
            default:
                break;
        }

        boolean isEnabled = ap.isWifiApEnabled();
        StringBuilder sb = new StringBuilder();
        sb.append(isEnabled);
        sb.append("\n");
        sb.append("old: " + ap.getOldWifiConguration().SSID);
        sb.append("\n");
        WifiConfiguration config = ap.getWifiApConfiguration();
        sb.append("new: ");

        sb.append(config == null ? "null" : config.SSID);
        sb.append("\n");
        //sb.append("local ip: ");
        sb.append(ap.getLocalIpAdress());
        sb.append("\n");
        sb.append(ap.getServerIpAdress());
		sb.append("ping " + wifi.pingSupplicant());
        sb.append("\n");
        sb.append("\n");
        sb.append(wifi.getConnectionInfo().toString());
        sb.append("\n");
        sb.append("\n" + wifi.getDhcpInfo().toString());
        tv.setText(sb.toString());

    }
*/


    private class WifiChatHandler extends Handler {

        private Context context;

        public WifiChatHandler(Context context) {
            this.context = context;
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case WifiChatService.MESSAGE_READ:
                case WifiChatService.MESSAGE_WRITE:
                    BaseMessage message = (BaseMessage) msg.obj;
                    //更新聊天内容
                    chatRoomFragment.addData(message);
                    break;
                case WifiChatService.MESSAGE_SCAN_RESULTS_AVAILABLE:
                    showToast("wifi搜索完成");
                    mainFragment.setScanResult(service.getScanResult());
                    break;
                case WifiChatService.MESSAGE_STATE_CHANGE:
                    Log.d("MainActivity","WifiChatService状态改变");
                    break;
                case WifiChatService.MESSAGE_CONNECT_FAILED:
                    showToast("连接失败");
                    break;
                case WifiChatService.MESSAGE_CONNECTED:
                    showToast("握手成功");
                    toChatRoom();
                    break;
                case WifiChatService.MESSAGE_START:
                    showToast("等待其他人接入");
                    break;
                default:
                    //showToast("message what : " + msg.what);
                    break;

            }
        }
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
